package com.bw.a2207a.view

import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentPagerAdapter
import com.alibaba.android.arouter.facade.annotation.Route
import com.bw.a2207a.R
import com.bw.a2207a.base.BaseActivity
import com.bw.a2207a.databinding.ActivityGoodsDetailBinding
import com.bw.a2207a.view.fragment.CommentFragment
import com.bw.a2207a.view.fragment.GoodsDetailFragment
import com.bw.a2207a.viewmodel.GoodsDetailViewModel
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayout.OnTabSelectedListener


@Route(path = "/main/GoodsDetailActivity")
class GoodsDetailActivity :
    BaseActivity<ActivityGoodsDetailBinding, GoodsDetailViewModel>(R.layout.activity_goods_detail) {

    val fragmentList = mutableListOf<Fragment>()
    override fun initData() {
    }

    override fun initView() {
        val id = intent.getIntExtra("id", 0)
//        viewModel.getGoodsDetail(id)


        binding.detailTab.addTab(binding.detailTab.newTab().setText("详情"))
        binding.detailTab.addTab(binding.detailTab.newTab().setText("评论"))

        binding.detailTab.addOnTabSelectedListener(object : OnTabSelectedListener {
            override fun onTabSelected(tab: TabLayout.Tab) {
                //切换页面
                binding.vp.currentItem = tab.position
            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {}
            override fun onTabReselected(tab: TabLayout.Tab?) {}

        })

        fragmentList.add(GoodsDetailFragment.newInstance(id))
        fragmentList.add(CommentFragment())

        binding.vp.adapter = object : FragmentPagerAdapter(supportFragmentManager) {
            override fun getCount(): Int {
                return fragmentList.size
            }

            override fun getItem(position: Int): Fragment {
                return fragmentList.get(position)
            }

        }

//        binding.vp.setOnPageChangeListener(object : OnPageChangeListener {
//            override fun onPageScrolled(
//                position: Int,
//                positionOffset: Float,
//                positionOffsetPixels: Int
//            ) {
//            }
//
//            override fun onPageSelected(position: Int) {
//                tabLayout.hideMsg(position)
//                if (position == 1) {
//                    //隐藏小红点
//                    tabLayout.hideMsg(1)
//                }
//                //vp页面切换时，调用tabLayout切换方法
//                tabLayout.setCurrentTab(position)
//                currentIndex = position
//                changeTitle(position)
//            }
//
//            override fun onPageScrollStateChanged(state: Int) {}
//        })


    }
}